var cityData = require('../../utils/city.js');
var app = getApp();
var page
var size
var listsize = 0;
// page/one/index.js
Page({
    data: {
        content: [],
        nv: [],
        px: [],
        qyopen: false,
        qyshow: false,
        nzopen: false,
        pxopen: false,
        nzshow: false,
        pxshow: false,
        isfull: false,
        cityleft: cityData.getCity(),
        citycenter: {},
        cityright: {},
        select1: '',
        select2: '',
        shownavindex: '',
        imgurl:app.globalData.imgurl,
        keyword:''
    },
    onLoad: function () {
      // 定位
      // app.getCity(this)
        this.setData({
            nv: ['衣服', '裤子', '内衣', '服饰', '衣服', '裤子', '内衣', '服饰', '衣服', '裤子', '内衣', '服饰'],
            px: ['短期兼职', '长期兼职', '周末兼职']
        })
        this.getBanner();//轮播图
    },
   onShow:function(){
      var that = this;
     wx.getStorage({
        key: 'selectedCity',
        success: function(res) {
          var filters = {
            cityId: res.data.areaid,
            deatilName: ''
          }
          that.setData({
            selectedCity: res.data,
            filters: filters,
          })
          that.getQuyuList(res.data);
          that.getpartType();
          that.getpartCycle();

          page = 1;
          that.deatilList(filters)
        },
        fail:function(){
          //  缓存请求失败，设置默认城市
          var filters = {
            cityId: 'c16',
            deatilName:''
          }
          that.setData({
            selectedCity: { areaid:'c16',name:'太原市'},
            filters: filters,
          })
          that.getQuyuList({areaid:'c16',name:'太原市'});
          that.getpartType();
          that.getpartCycle();
          page = 1
          that.deatilList(filters)
        }      
      })
    },
   getBanner:function(){
     var that = this;
    wx.request({
      url: app.globalData.url + 'bannerList',
      success:function(res){
        console.log(res)
        if(res.data.success){
          that.setData({
            bannerlist:res.data.body.bannerList
          })
        }
      }
    })
   },
    // 获取区域列表
   getQuyuList:function(e){
     var that = this;
    wx.request({
      url: app.globalData.url + 'getArea?parentId=' + e.areaid,
      success:function(res){
        var cityleft = [{
          name:'不限',
          areaId:'',
          selectedClass: 'selected'
        }]
        that.setData({
          cityleft: cityleft.concat(res.data)
        })
      },
      fail:function(){},
      complete:function(){}
    })
   },
  //  选择县区
   selectleft: function (e) {
      var that= this;
      var o = e.currentTarget.dataset;
      var filters = that.data.filters;
      filters.countyId = o.areaid;

      // 选中样式
      var cityleft = that.data.cityleft;
      for(var i = 0; i < cityleft.length; i++){
        cityleft[i].selectedClass = '';
      }
      cityleft[o.index].selectedClass = 'selected'
      setTimeout(function () {
        that.listqy()
      }, 500)
      //  刷新列表 
      page = 1;
      that.deatilList(filters)
     
      that.setData({
        filters: filters, // 刷新列表参数
        cityleft: cityleft
      })
   },
  //  获取关键字长度
   getkeywordLength:function(e){
      var keyword = e.detail.value;
      if(keyword.length){
        var showdelete = true
      }else{
        var showdelete = false
      }
      this.setData({
        showdelete: showdelete
      })
   },
   getkeyword:function(e){
     var keyword = e.detail.value;
     var filters = this.data.filters;
     filters.deatilName = keyword;
     page = 1;
     this.deatilList(filters)
     this.setData({
       filters: filters
     })
   },
  // 获取分类
   getpartType: function () {
      var that = this;
      wx.request({
        url: app.globalData.url + 'partType',
      success:function(res){
        var partTypes = [{
          name: '不限',
          typeId: '',
          selectedClass: 'selected'
        }]
        that.setData({
          partTypes: partTypes.concat(res.data)
        })
      },
      fail:function(){},
      complete:function(){}
    })
   },
   //  选择分类
   setpartType:function(e){
     var that = this
    var o = e.currentTarget.dataset;
    var filters = this.data.filters
    filters.typeId = o.typeid;
    // 选中样式
    var partTypes = this.data.partTypes;
    for (var i = 0; i < partTypes.length; i++) {
      partTypes[i].selectedClass = '';
    }
    partTypes[o.index].selectedClass = 'selected'
    this.setData({
      filters: filters,
      partTypes:partTypes
    })
   
    setTimeout(function () {
      that.list()
    }, 500)
    //  刷新列表 
    page = 1;
    this.deatilList(filters)
   },
  //  清空分类
   clearType:function(){
     var filters = this.data.filters
     filters.typeId ='';
     this.list()
     this.deatilList(filters)
   },
   // 获取时间
   getpartCycle: function () {
     var that = this;
     wx.request({
       url: app.globalData.url + 'partCycle',
       success: function (res) {
         var partCycles = [{
           name: '不限',
           cycleId: '',
           selectedClass:'selected'
         }]
         that.setData({
           partCycles: partCycles.concat(res.data)
         })
       },
       fail: function () { },
       complete: function () { }
     })
   },
   //  选择时间
   setpartCycle: function (e) {
     var that = this
     var o = e.currentTarget.dataset;
     var filters = this.data.filters
     filters.cycleId = o.cycleid;

     // 选中样式
     var partCycles = this.data.partCycles;
     for (var i = 0; i < partCycles.length; i++) {
       partCycles[i].selectedClass = '';
     }
     partCycles[o.index].selectedClass = 'selected'
     this.setData({
       filters: filters,
       partCycles: partCycles
     })
     
setTimeout(function(){
  that.listpx()
},500)
//  刷新列表 
page = 1;
this.deatilList(filters)
   },

  //  清空时间
   clearCycle: function () {
     var filters = this.data.filters
     filters.cycleId = '';
     this.listpx()
     this.deatilList(filters)
   },

  //  列表加载
   deatilList:function(e){
    wx.showLoading({
      title: '正在加载',
      mask: true,
    })
    
    var that = this;
    if (page == 1) {
      var list = []
    } else {
      var list = that.data.deatilList
    }
    wx.request({
      // url:'http://192.168.11.39:8080/jeeplus/a/part/deatilList?cityId=c16&pageNo='+ page,
      url: app.globalData.url + 'deatilList?pageNo='+ page,
      data:e,
      success:function(res){
        var _list = res.data.body.deatilList;
        listsize = _list.length;
        size = res.data.body.size;
        for(var i = 0; i <_list.length; i++){
          if (_list[i].startDate != null){
            _list[i].startDatastr = that.datestr(_list[i].startDate)
          }
        }
        if (_list != ''){
         list = list.concat(_list)
        }
        if(listsize == 0){
          var more = '没有更多啦'
        }else{
          var more = '加载更多'
        }

        page++;
        that.setData({
          deatilList: list,
          more: more
        })
      },
      fail:function(){
        that.setData({
          deatilList: list,
        })
      },
      complete:function(){
        wx.hideLoading();
        setTimeout(function(){
          wx.stopPullDownRefresh()
        },1000)
      }
    })
   },
  //  区域
    listqy: function (e) {
        if (this.data.qyopen) {
            this.setData({
                qyopen: false,
                nzopen: false,
                pxopen: false,
                nzshow: true,
                pxshow: true,
                qyshow: false,
                isfull: false,
                shownavindex: 0
            })
        } else {
            this.setData({
                qyopen: true,
                pxopen: false,
                nzopen: false,
                nzshow: true,
                pxshow: true,
                qyshow: false,
                isfull: true,
                shownavindex: e.currentTarget.dataset.nav
            })
        }

    },
    // 分类
    list: function (e) {
        if (this.data.nzopen) {
            this.setData({
                nzopen: false,
                pxopen: false,
                qyopen: false,
                nzshow: false,
                pxshow: true,
                qyshow: true,
                isfull: false,
                shownavindex: 0
            })
        } else {
            this.setData({
              // content: this.data.partTypes,
                nzopen: true,
                pxopen: false,
                qyopen: false,
                nzshow: false,
                pxshow: true,
                qyshow: true,
                isfull: true,
                shownavindex: e.currentTarget.dataset.nav
            })
        }
    },

    // 时间
    listpx: function (e) {
        if (this.data.pxopen) {
            this.setData({
                nzopen: false,
                pxopen: false,
                qyopen: false,
                nzshow: true,
                pxshow: false,
                qyshow: true,
                isfull: false,
                shownavindex: 0
            })
        } else {
            this.setData({
              // content: this.data.partCycles,
                nzopen: false,
                pxopen: true,
                qyopen: false,
                nzshow: true,
                pxshow: false,
                qyshow: true,
                isfull: true,
                shownavindex: e.currentTarget.dataset.nav
            })
        }
    },
   
    selectcenter: function (e) {

        this.setData({
            cityright: this.data.citycenter[e.currentTarget.dataset.city],
            select2: e.target.dataset.city
        });
    },
    hidebg: function (e) {

        this.setData({
            qyopen: false,
            nzopen: false,
            pxopen: false,
            nzshow: true,
            pxshow: true,
            qyshow: true,
            isfull: false,
            shownavindex: 0
        })
    },
    // date年月日
    datestr:function(e){
      var date_arr = e.split('-');
     return date_arr[0]+'年' + date_arr[1]+'月' + date_arr[2]+'日'
    },
    onPullDownRefresh:function(){
      page = 1;
      var filters = this.data.filters
      this.deatilList(filters)
    },
    onReachBottom:function(){
      var filters = this.data.filters
      if(listsize > 0){
        this.deatilList(filters)
      }
   
    },

})